﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookDBM
{
    public partial class Form1 : Form
    {
        [Serializable]
        class BookInfo
        {
            public int ID;
            public double Price;
            public string Title, Author, Publisher;

            public override string ToString()
            {
                return Title;
            }
        }
        static Dictionary<int, BookInfo> infolist;

        public Form1()
        {
            infolist = new Dictionary<int, BookInfo>();
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Loaddb();
            RefreshList();
        }

        void RefreshList()
        {
            booklist.Items.Clear();
            foreach (var item in infolist)
            {
                booklist.Items.Add(item.Value);
            }
        }

        void Loaddb()
        {
            if (File.Exists("db.json"))
            {
                string data = File.ReadAllText("db.json");
                infolist = JsonConvert.DeserializeObject<Dictionary<int, BookInfo>>(data);
            }
            else
            {
                infolist = new Dictionary<int, BookInfo>();
            }
        }

        void Savedb()
        {
            string data = JsonConvert.SerializeObject(infolist);
            File.WriteAllText("db.json", data);
        }

        private void savebutton_Click(object sender, EventArgs e)
        {
            int id = (int)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
            BookInfo i = new BookInfo { Title = titlebox.Text, Author = authorbox.Text, Price = (double)pricebox.Value, ID = id, Publisher = pubbox.Text };

            if (booklist.SelectedItem == null)
            {
                infolist.Add(id, i);
                booklist.Items.Add(i);
            }
            else
            {
                var s = (BookInfo)booklist.SelectedItem;
                i.ID = s.ID;
                infolist[i.ID] = i;
                RefreshList();
            }
        }

        private void booklist_SelectedValueChanged(object sender, EventArgs e)
        {
            if (booklist.SelectedItem == null)
            {
                titlebox.Text = "";
                authorbox.Text = "";
                pricebox.Value = 0;
                pubbox.Text = "";
            }
            else
            {
                var s = (BookInfo)booklist.SelectedItem;
                titlebox.Text = s.Title;
                authorbox.Text = s.Author;
                pricebox.Value = (decimal)s.Price;
                pubbox.Text = s.Publisher;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Savedb();
        }

        private void deletebutton_Click(object sender, EventArgs e)
        {
            if (booklist.SelectedItem == null)
            {
                MessageBox.Show("未选中要删除的目标");
                return;
            }
            var s = (BookInfo)booklist.SelectedItem;
            titlebox.Text = "";
            authorbox.Text = "";
            pricebox.Value = 0;
            pubbox.Text = "";
            infolist.Remove(s.ID);
            booklist.Items.Remove(s);
        }

        private void booklist_DoubleClick(object sender, EventArgs e)
        {
            if (booklist.SelectedItem != null)
                booklist.SetSelected(booklist.SelectedIndex, false);
        }
    }
}